<?php
namespace Modules\Order\Services\Api;

use App\Helpers\ApiResponse;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Schema;
use Modules\Order\Entities\Goods;
use Modules\Order\Entities\MallSetting;
use Modules\Order\Entities\User;
use Modules\Order\Entities\UserAddress;
use Modules\Order\Entities\UserLevel;

class BaseService
{
    use ApiResponse;

    /**
     * 每页显示多少条
     *
     * @return array|int|string|null
     */
    public function perPage()
    {
        $perPage = Cookie::get('admin_per_page') ?? 10;

        return $perPage < 100 ? $perPage : 100;
    }

    /**
     * @param $query
     * @return array
     * @author tan bing
     * @date 2021-07-20 17:14
     */
    public function returnPageData($query)
    {
        $count = $query->total();
        $data  = $query->items();
        return compact('count', 'data');
    }

    /**
     * 获取请求参数
     * @return mixed
     * @author tan bing
     * @date 2021-07-20 17:33
     */
    public function getRequestParam()
    {
        return request()->input();
    }

    /**
     * 获取地址信息
     * @param array $where [["user_id", "=", 1], ["is_default", "=", 1]]
     * @return array
     */
    public function getUserAddress(array $where)
    {
        $value = [];
        if(Schema::hasTable('user_address')){
            $value = UserAddress::where($where)->first();
            if(!$value) return [];
            return $value->toArray();
        }
        return $value;
    }

    /**
     * 获取用户最新信息
     * @param int $id
     * @return mixed
     */
    public function getUserInfo(int $id)
    {
        return User::where("id", $id)->first();
    }

    /**
     * 获取用户等级信息
     * @param int $id 等级ID
     * @return array
     */
    public function getUserLevel(int $id)
    {
        $value = [];
        if(Schema::hasTable('user_level')){
            $where = [
                ["id", "=", $id],
                ["deleted_at", "=", null],
            ];
            $value = UserLevel::where($where)->select("id","name","discount")->first();
            if(!$value) return [];
            return $value->toArray();
        }
        return $value;
    }

    /**
     * 获取商城设置表信息
     * @return string
     */
    private function getMallSetting()
    {
        $value = '';
        if(Schema::hasTable('mall_setting')){
            $value = MallSetting::where("id", 1)->first();
        }
        return $value;
    }

    /**
     * 获取商城设置表字段参数
     * @param $july string 字段名
     * @return string
     */
    public function getMallSettingValue(string $july): string
    {
        $value = '';
        $setting = $this->getMallSetting();
        if($setting != "") $value = $setting->$july ?? "";
        return $value;
    }

    /**
     * 获取可用的支付方式
     * @return array
     */
    public function getPayment()
    {
        $data = [
            [
                "name" => "微信支付",
                "code" => "wechat",
                "pay_type" => 0,
                "icon" => asset('/Modules/Order/images/icon-online.png')
            ]
        ];
        $paymentStr = $this->getMallSettingValue("payment");
        if($paymentStr != ""){
            $payment = json_decode($paymentStr, true);
            if(isset($payment["huodao"]) && $payment["huodao"] == "on"){
                $data[] = [
                    "name" => "货到付款",
                    "code" => "huodao",
                    "pay_type" => 2,
                    "icon" => asset('/Modules/Order/images/icon-huodao.png')
                ];
            }
            if(isset($payment["balance"]) && $payment["balance"] == "on"){
                $data[] = [
                    "name" => "余额支付",
                    "code" => "balance",
                    "pay_type" => 1,
                    "icon" => asset('/Modules/Order/images/icon-balance.png')
                ];
            }
        }
        return $data;
    }

    /**
     * 获取积分使用信息
     * @return array
     */
    public function getIntegral()
    {
        $integral = [
            "forehead" => 0,  // 可以抵扣金额
            "forehead_integral" => 0, // 使用积分数量,
            "integration" => '', // 积分使用规则文字
            "scale" => 0, // N积分抵扣1元
        ];
        $mallSetting = $this->getMallSetting();
        if($mallSetting == "") return $integral;
        $integral["integration"] = $mallSetting->integration ?? "";
        $integral["scale"] = $mallSetting->integral ?? 0;
        return $integral;
    }

    /**
     * 获取商品信息
     * @param int $goods_id
     */
    public function getGoodsInfo(int $goods_id)
    {
        $value = '';
        if(Schema::hasTable('goods')){
            $value = Goods::where([["id", "=", $goods_id], ["status", "=", 1]])->first();
        }
        return $value;
    }
}